#include "BieuThucChia.h"


BieuThucChia::BieuThucChia()
{
}

BieuThucChia::BieuThucChia(BieuThuc* bt1, BieuThuc* bt2)
{
	this->bieuThuc1 = bt1;
	this->bieuThuc2 = bt2;
}

BieuThucChia::~BieuThucChia()
{
	delete bieuThuc1;
	delete bieuThuc2;
}

double BieuThucChia::tinhGiaTri(double x)
{
	return bieuThuc1->tinhGiaTri(x) / bieuThuc2->tinhGiaTri(x);
}

//BieuThuc* BieuThucChia::tinhDaoHam(){
//	BieuThuc* daoHambt1 = bieuThuc1->tinhDaoHam();
//	BieuThuc* daoHambt2 = bieuThuc2->tinhDaoHam();
//
//	BieuThuc* tich1 = new BieuThucTich(daoHambt1, bieuThuc2);
//	bieuThuc1->daoDauBieuThuc();
//	BieuThuc* tich2 = new BieuThucTich(daoHambt2, bieuThuc1);
//
//	BieuThuc* kqTu = new BieuThucTong(tich1, tich2);
//	BieuThuc* kqMau = new BieuThucTich(bieuThuc2, bieuThuc2);
//
//	BieuThuc* kq = new BieuThucChia(kqTu, kqMau);
//	return kq;
//}

BieuThuc* BieuThucChia::timDaoHam()
{
	BieuThuc* daoHambt1 = bieuThuc1->tinhDaoHam();
	BieuThuc* daoHambt2 = bieuThuc2->tinhDaoHam();

	BieuThuc* tich1 = new BieuThucTich(daoHambt1, bieuThuc2);
	bieuThuc1->daoDauBieuThuc();
	BieuThuc* tich2 = new BieuThucTich(daoHambt2, bieuThuc1);

	BieuThuc* kqTu = new BieuThucTong(tich1, tich2);
	BieuThuc* kqMau = new BieuThucTich(bieuThuc2, bieuThuc2);

	BieuThuc* kq = new BieuThucChia(kqTu, kqMau);
	return kq;
}

void BieuThucChia::daoDauBieuThuc()
{
	bieuThuc1->daoDauBieuThuc();
}

string BieuThucChia::xuatChuoi()
{
	string kqbt1 = bieuThuc1->xuatChuoi();
	string kqbt2 = bieuThuc2->xuatChuoi();
	if (kqbt1 == "")
		return "";
	else
	{
		if (kqbt2 == "")
			return kqbt1;
		else
			return "(" + kqbt1 + "/" + kqbt2 + ")";
	}
}
